﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Ext.Net;
using System.IO;
using YueDa.Web.Common;
using System.Data;
public partial class Excel_Files : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Cookies.getCookies("uid") != "1")
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!X.IsAjaxRequest)
        {
            DataBind();
        }
    }
    protected void ShowExcelDialog(object sender, DirectEventArgs e)
    {
        Window1.Show();
    }
    protected void UploadFiles(object sender, DirectEventArgs e)
    {
        HttpPostedFile postedFile = txtFileUpload.PostedFile;
        string fileName = System.IO.Path.GetFileName(postedFile.FileName);
        string fileExt = System.IO.Path.GetExtension(fileName); 
        Guid guid = Guid.NewGuid();
        int OwnerID = int.Parse(Cookies.getCookies("uid"));

        postedFile.SaveAs(Server.MapPath("~/UpLoad/" + guid + fileExt));

        DataTable dt = new DataTable();

        dt = ExcelHelper.ImportExceltoDt(Server.MapPath("~/UpLoad/" + guid + fileExt), 0, -1);
         
        int isRows = dt.Rows.Count;
        using (var m = new ACEntities())
        {
            XslFiles f = new XslFiles();
            f.Filepath = "~/UpLoad/" + guid + fileExt;
            f.FileName = fileName;
            f.isRows = isRows;
            f.isExport = 0;
            f.inTime = DateTime.Now;
            m.AddToXslFiles(f);
            m.SaveChanges();
            
        }
        DataBind();
        Window1.Hide();
    }
    protected void DataBind()
    {
        using (var m = new ACEntities())
        {
            var q = from c in m.XslFiles
                    orderby c.ID descending
                    select c;
            Store1.DataSource = q;
            Store1.DataBind();
        }
    }
    protected void Imports(object sender, DirectEventArgs e)
    {
        int id = int.Parse(e.ExtraParams["id"].ToString());
        using (var m = new ACEntities())
        {
            var q = (from c in m.XslFiles
                    where c.ID == id
                    select c).FirstOrDefault();
            DataTable dt = ExcelHelper.ImportExceltoDt(Server.MapPath(q.Filepath), 0, -1);
            int start = int.Parse(q.isExport.ToString());
            int limit = dt.Rows.Count;
            DateTime times = DateTime.Now;
            int t = start;
            for (int i = start; i < start+10000; i++)
            {
                if (limit-1 >= i)
                { 
                    DataRow dr = dt.Rows[i];
                    CustomersUploads cu = new CustomersUploads();
                    cu.FileID = id;
                    cu.CTMID = int.Parse(dr[0].ToString());
                    cu.CTMCompany = dr[1].ToString();
                    cu.CTMUsername = "";
                    cu.CTMPhone = dr[2].ToString();
                    cu.CTMFax = dr[3].ToString();
                    cu.CTMEmail = dr[5].ToString();
                    cu.CTMAddress = dr[4].ToString();
                    cu.SmallClass = dr[7].ToString() + "-" +
                                    dr[6].ToString().Substring(0, dr[6].ToString().IndexOf('('));
                    cu.BigClass = dr[8].ToString();
                    cu.isOut = 0;
                    cu.inTime = times;
                    m.AddToCustomersUploads(cu);
                    t++;
                }
                
            } 
            q.isExport = t;
            m.SaveChanges();
            
            DataBind();
            
        }
    }
}